/*
 * bsp_can.c
 *
 *  Created on: 2025 Jan 31
 *      Author: SFLY
 */
#include "bsp_can.h"
#include "bsp_board_config.h"
extern __interrupt void canfdISR(void);

void Bsp_CAN_PIN_Init(void)
{
      GPIO_MuxConfig(16, GPIO_16_CANB_TX, GPIO_PIN_TYPE_STD, GPIO_QUAL_ASYNC);
      GPIO_setDirectionMode(16, GPIO_DIR_MODE_OUT);
      GPIO_MuxConfig(7, GPIO_7_CANB_RX, GPIO_PIN_TYPE_STD, GPIO_QUAL_ASYNC);
      GPIO_setDirectionMode(7, GPIO_DIR_MODE_IN);
}

void Bsp_CAN_Init(void)
{
    Bsp_CAN_PIN_Init();
    CAN_Init_Config canInit;
    CANFD_Config canFd;
    // enable module clock
    SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_CANB);
    //Initializes CAN parameters.
    CAN_structInit(&canInit);
    CANFD_structInit(&canFd);
    canInit.canFDControl = CAN_FD_ENABLE;
    canFd.TDC = CAN_FD_TDC_ENABLE;
    canFd.SSPOffset = 16;
    canInit.ptrCanFd = &canFd;
    // Software reset.Set the bit rate and filter need the can in software reset mode.
    CAN_enableStatReset(CANFDB_BASE);
    //Initialize CAN module
    CAN_initModule(CANFDB_BASE, &canInit);
   // CAN_setAcceptFilter(CANFDB_BASE, CAN_ACF1, CAN_ID_STD, MOTOR_CAN_ID, MOTOR_CAN_ID_MASK);
    // (bundrate = pclk/s_prescale/(s_seg1+2 + s_seg2+1))
    //baund = 150000000/3/(38+2+9+1)
    //s_prescale:3
    //s_seg1: 38
    //s_seg2:9
    //s_sjw:9
    CAN_setBitTimingSlow(CANFDB_BASE, 5, 38, 9, 9);  //500K 仲裁
    // (bundrate = pclk/s_prescale/(s_seg1+2 + s_seg2+1))
    //baund = 150000000/2/(18+2+4+1)
    //f_prescale:1
    //f_seg1: 18
    //f_seg2:4
    //f_sjw:4
    CANFD_setBitTimingFast(CANFDB_BASE, 2, 18, 4, 4);// 2M  数据
    // CAN enters normal communication mode.
    CAN_disableStatReset(CANFDB_BASE);
    // Enable CAN Interrupt.
    CAN_enableInterrupt(CANFDB_BASE, CAN_INT_ALL);
    DEVICE_DELAY_US(100);
    Interrupt_register(INT_CANB, &canfdISR);
    Interrupt_enable(INT_CANB);
}

void Bsp_CAN_SetID(uint32_t motor_id)
{
    CAN_setAcceptFilter(CANFDB_BASE, CAN_ACF1, CAN_ID_STD, motor_id, MOTOR_CAN_ID_MASK);
}

void Bsp_CAN_SendMessage(uint32_t motor_id,uint8_t *data)
{
      CAN_TBUF_Ctrl tx1Ctrl;
      tx1Ctrl.TTSEN = CAN_TTSEN_DISABLE;
      tx1Ctrl.BRS = CANFD_BRS_FAST;
      tx1Ctrl.FDF = CAN_FDF_CAN20;
      tx1Ctrl.IDE = CAN_IDE_STANDARD;
      tx1Ctrl.RTR = CAN_RTR_DATA;
      tx1Ctrl.DLC = CAN_DLC8;
      CAN_fillMessage(CANFDB_BASE, CAN_TX_BUF_PTB,motor_id, &tx1Ctrl, data);
}

uint32_t Bsp_CAN_RecvMessage(uint8_t *data)
{
        CAN_RBUF_Ctrl rxCtrl;
        uint32_t motor_id = 0;
        CAN_readMessageWithID(CANFDB_BASE,&motor_id, &rxCtrl,data);
        return motor_id;
}

void Bsp_CANFD_SendMessage(uint32_t motor_id,uint8_t *data)
{
        CAN_TBUF_Ctrl tx1Ctrl;
        tx1Ctrl.TTSEN = CAN_TTSEN_DISABLE;
        tx1Ctrl.BRS = CANFD_BRS_FAST;
        tx1Ctrl.FDF = CAN_FDF_CANFD;
        tx1Ctrl.IDE = CAN_IDE_STANDARD;
        tx1Ctrl.RTR = CAN_RTR_DATA;
        tx1Ctrl.DLC = CAN_DLC64;
        CAN_fillMessage(CANFDB_BASE, CAN_TX_BUF_PTB,motor_id, &tx1Ctrl, data);
}


void Bsp_CANFD_RecvMessage(uint32_t motor_id,uint8_t *data)
{
        CAN_RBUF_Ctrl rxCtrl;
        CAN_readMessageWithID(CANFDB_BASE,&motor_id, &rxCtrl,data);
}


